//
//  KYAppInfoManager.m
//  ipa_dumper491
//
//  Created by philip on 12/9/15.
//  Copyright © 2015 7659. All rights reserved.
//

#import "KYAppInfoManager.h"
#import "KYApplicationInfo.h"

@implementation KYAppInfoManager
+(NSArray *)installedAppDetailInfo
{
    Class appWSClass = NSClassFromString(@"LSApplicationWorkspace");
    NSObject* workspace = [appWSClass performSelector:@selector(defaultWorkspace)];
    id data = nil;
    NSMutableArray* result = nil;
    
    if ([workspace respondsToSelector:@selector(allInstalledApplications)]) {
        data = [workspace performSelector:@selector(allInstalledApplications)];
        if ([data count] > 0) {
            result = [NSMutableArray array];
            for (id item in data) {
                if ([@"User" isEqualToString:[item valueForKey:@"applicationType"]]) {
                    
                    NSLog(@"%@", item);
                    KYApplicationInfo* info = [[KYApplicationInfo alloc] init];
                    
                    info.Name = [item valueForKey:@"localizedName"] ? :[item valueForKey:@"localizedShortName"] ;
                    info.bundleIdentifier = [item valueForKey:@"bundleIdentifier"];
                    info.bundleURLString = [[item valueForKey:@"bundleURL"] absoluteString];
                    info.bundleExecutable = [item valueForKey:@"bundleExecutable"];
                    info.appTags = [item valueForKey:@"appTags"];
                    info.dataContainerURLString = [[item valueForKey:@"dataContainerURL"] absoluteString];
                    info.signerIdentity = [item valueForKey:@"signerIdentity"];
                    
                    [result addObject:info];
                }
            }
        }
    }
    
    return result;
}

+(NSString *)bundleURL4bundleIdentifier:(NSString*)bundleIdentifier
{
    Class appWSClass = NSClassFromString(@"LSApplicationWorkspace");
    NSObject* workspace = [appWSClass performSelector:@selector(defaultWorkspace)];
    id data = nil;
    
    if ([workspace respondsToSelector:@selector(allInstalledApplications)]) {
        data = [workspace performSelector:@selector(allInstalledApplications)];
        if ([data count] > 0) {
        
            for (id item in data) {
                if ([bundleIdentifier isEqualToString:[item valueForKey:@"bundleIdentifier"]]) {
                    return [item valueForKey:@"bundleURL"];
                }
            }
        }
    }
    
    return nil;
}
@end
